/*
Assignment 1
Class ID        : SE0691
Group           : 1
Due Date        : 20 November 2012
I declare that this assignment is my own work
in accordance with FPT Policy.
*/
package edu.fpt.dal;

import edu.fpt.hib.Apartment;
import edu.fpt.hib.HibernateUtil;
import edu.fpt.hib.Services;
import edu.fpt.hib.Users;
import java.util.ArrayList;
import java.util.StringTokenizer;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Admin
 */
public class ServicesDAL {
    
    public static ArrayList<Services> listAll() {
        Session session = null;
        Transaction tran;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            ArrayList<Services> list;
            Query query = session.createQuery("from Services");
            list = (ArrayList<Services>) query.list();
            tran.commit();
            return list;
        } finally {
            session.close();
        }
    }
    
    public static Services getByServicesID(String id) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Query query = session.createQuery("from Services where serviceid = '" + id + "'");
            Services service = (Services) query.uniqueResult();
            tran.commit();
            return service;
        } finally {
            session.close();
        }
    }
    
    public static boolean inList(ArrayList<String> list, String id) {
        for (String str : list) {
            if (str.equals(id)) {
                return true;
            }
        }
        return false;
    }
    
    public static String removeFromList(String list, String id) {
        StringTokenizer stToken = new StringTokenizer(list, " ");
        ArrayList<String> ser = new ArrayList<String>();
        while (stToken.hasMoreElements()) {
            ser.add(stToken.nextToken().toString());
        }
        String result = "";
        for (String str : ser) {
            if (!str.equals(id)) {
                result += str + " ";
            }
        }
        return result;
    }
    
    public static ArrayList<Services> listNotUsing(String userid) {
        Users users = UsersDAL.getUsersHQL(userid);
        Apartment apart = ApartmentDAL.getApartmentByUserHQL(userid);
        StringTokenizer stToken = new StringTokenizer(apart.getUsingServices(), " ");
        ArrayList<String> ser = new ArrayList<String>();
        ArrayList<Services> list = listAll();
        ArrayList<Services> result = new ArrayList<Services>();
        while (stToken.hasMoreElements()) {
            ser.add(stToken.nextToken().toString());
        }
        for (Services services : list) {
            if (!inList(ser, services.getServiceId())) {
                result.add(services);
            }
        }
        return result;
    }
    
    public static void update(Services entity) {
        Session session = null;
        Transaction tran = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            session.update(entity);
            tran.commit();
        } finally {
            session.close();
        }
    }
    
    public static void insert(Services entity) {
        Session session = null;
        Transaction tran = null;
        try {
            session = HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            session.save(entity);
            tran.commit();
        } finally {
            session.close();
        }
    }
    
    public static void delete(String id) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Services service = (Services) session.get(Services.class, id);

            session.delete(service);
            tran.commit();
        } finally {
            session.close();
        }
    }
    
    public static boolean exist(String serviceID) {
        Session session = null;
        Transaction tran = null;
        try {
            session = (Session) HibernateUtil.getSessionFactory().openSession();
            tran = session.beginTransaction();
            Query query = session.createQuery("from Services where serviceid = :id");
            query.setParameter("id", serviceID);
            Services ser = (Services) query.uniqueResult();
            tran.commit();
            return ser != null;
        } finally {
            session.close();
        }
    }
}
